import { _decorator, Component, director, Enum, find, Node } from 'cc';
import { PieceManagerBase } from './PieceManagerBase';
import { GameManager } from './GameManager';
const { ccclass, property } = _decorator;

@ccclass('MoveOrEatManager')
export class MoveOrEatManager extends Component {

    @property(String)
    type: 'MOVE' | 'EAT' = 'MOVE';

    targetNode: PieceManagerBase = null;
    gameManager: GameManager;
    posX: number = 0;
    posY: number = 0;

    start() {
        this.gameManager = find('Canvas/GameManager').getComponent(GameManager);
        this.node.on(Node.EventType.TOUCH_START, this.confirmChoose, this);
    }
    async confirmChoose(event: any) {
        this.clearMoveOrEatNode();
        let node = event.target;
        // let nodeController = node.getComponent(NodeController);
        this.targetNode.node.setPosition(node.getPosition());
        this.targetNode.freeNode();
        this.targetNode.posX = this.posX;
        this.targetNode.posY = this.posY;
        this.targetNode.initPiece();
        this.targetNode = null;
        this.gameManager.attackList = [];
        this.gameManager.moveList = [];
        this.gameManager.currentPiece = null;
    }
    clearMoveOrEatNode() {
        for (let index = 0; index < this.gameManager.attackList.length; index++) {
            const element = this.gameManager.attackList[index];
            this.gameManager.putAttackNode(element);
        }
        for (let index = 0; index < this.gameManager.moveList.length; index++) {
            const element = this.gameManager.moveList[index];
            this.gameManager.putAllowNode(element);
        }
    }
    update(deltaTime: number) {

    }
}

